﻿using System;
using System.Collections.Generic;
using System.Linq;
using ComicQ.Domain.Table;
using NHibernate.Linq;

namespace ComicQ.Controller
{
    public class RentController
    {
        public IList<Rent> GetAllRent()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Rent>().ToList();
            }
        }

        public IList<int> GetAllRentRate()
        {
            return GetAllRent().Select(rent => rent.Rate).ToList();
        }

        public int GetRentRate(Rent rent)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Rent>().ToList().
                    Where(r => r.Id == rent.Id).
                    Select(r => r.Rate).
                    Single();
            }
        }

        public int AddRent(Rent rent)
        {
            try
            {
                GetAllRent()
                    .Where(r => r.Rate == rent.Rate)
                    .Select(r => r)
                    .Single();

                return 0;
            }
            catch (Exception)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.Save(rent);
                        transaction.Commit();
                    }
                }

                return 1;
            }
        }

        public int EditRent(Rent newRent, int oldRate)
        {
            try
            {
                var checkRent = GetAllRent()
                    .Where(r => r.Rate == newRent.Rate)
                    .Select(r => r)
                    .Single();

                if (checkRent.Rate == oldRate)
                {
                    using (var session = NHibernateHelper.OpenSession())
                    {
                        using (var transaction = session.BeginTransaction())
                        {
                            session.SaveOrUpdate(newRent);
                            transaction.Commit();
                        }
                    }

                    return 1;
                }

                return 0;
            }
            catch (Exception)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.SaveOrUpdate(newRent);
                        transaction.Commit();
                    }
                }

                return 1;
            }
        }

        public void DeleteRent(Rent rent)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(rent);
                    transaction.Commit();
                }
            }
        }
    }
}
